//
//  SXReadRecommendViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/28.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXReadRecommendViewController.h"
#import "SXReadDetailListTableViewCell.h"
#import "SXReadDetailListATableViewCell.h"
#import "SXRequestManager.h"
#import "SXDataBaseManager.h"
#import "SXReadRecommendDetailViewController.h"

@interface SXReadRecommendViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation SXReadRecommendViewController

- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 153)];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        [_mainTableView registerClass:[SXReadDetailListTableViewCell class] forCellReuseIdentifier:@"SXReadDetailListTableViewCell"];
        [_mainTableView registerClass:[SXReadDetailListATableViewCell class] forCellReuseIdentifier:@"SXReadDetailListATableViewCell"];
    }
    return _mainTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _start = 0;
    _limit = 20;
    
    [SXDataBaseManager DBManage:nil dataName:@"readRecommend" manageType:(dataBaseManageTypeQueryData) getDataFromDict:^(NSData *data) {
        [self analysisData:data];
    }];
    
    [self requestData];
    
}

// 网络请求
- (void)requestData
{    
    [SXRequestManager requestWithType:GET urlString:kReadRecommendURLString parDic:nil finish:^(NSData *data) {
        if (_start == 0) {
            [SXDataBaseManager DBManage:data dataName:@"readRecommend" manageType:(dataBaseManageTypeStoreData) getDataFromDict:nil];
        }
        [self analysisData:data];
        
        } error:^(NSError *error) {
            NSLog(@"推荐阅读界面数据请求失败");
            [self removeLoadingView];
    }];
}

// 数据解析
- (void)analysisData:(NSData *)data
{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers) error:nil];
    NSArray *array = dict[@"推荐"];
    if (_start == 0) {
        [self.dataSourceArray removeAllObjects];
    }
    for (NSDictionary *dic in array) {
        SXReadModel *model = [[SXReadModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSourceArray addObject:model];
    }
    [self.mainTableView reloadData];
    [self removeLoadingView];
}

// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXReadModel *model = self.dataSourceArray[indexPath.row];
    if (model.imgnewextraArr.count > 0) {
        SXReadDetailListATableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXReadDetailListATableViewCell" forIndexPath:indexPath];
        cell.congfigureModel = model;
        return cell;
    }
    SXReadDetailListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SXReadDetailListTableViewCell" forIndexPath:indexPath];
    cell.congfigureModel = model;
    return cell;
}

// row高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXReadModel *model = self.dataSourceArray[indexPath.row];
    if (model.imgnewextraArr.count > 0) {
        return [SXReadDetailListATableViewCell heightForCell];
    }
    return [SXReadDetailListTableViewCell heightForCellWithModel:model];
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SXReadModel *model = self.dataSourceArray[indexPath.row];
    SXReadRecommendDetailViewController *detailVC = [[SXReadRecommendDetailViewController alloc] init];
    detailVC.docid = model.docid;
    [self.readRootVC.rootRightVC.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
